package com.huaxin.dss2.data.dao.base;

import com.huaxin.dss2.data.bean.DataValue;
import com.huaxin.dss2.util.Constant;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;

/**
 * 关系型数据库抽象类
 * @author : luqing
 * @date : 2018/11/21 10:13
 */
public abstract class BaseRDBDataDao extends BaseDataDao{

    /**
     * 关系型数据库的存储方法
     * @param jdbcTemplate 由于mysql采用分库方式，因此需要此参数
     * @param saveList 数据集合
     * @param dataDate 数据时间
     * @throws Exception 抛出异常
     */
    protected abstract void batchUpdate(JdbcTemplate jdbcTemplate,List<DataValue> saveList, Date dataDate) throws Exception;

    protected BatchPreparedStatementSetter batchPreparedStatementSetter(List<DataValue> saveList,Date dataDate){
        return new BatchPreparedStatementSetter() {
            @Override
            public void setValues(PreparedStatement ps, int index) throws SQLException {
                DataValue dataValue = saveList.get(index);
                String[] fullDataCode = dataValue.getDataCode().split("_");
                ps.setString(1, fullDataCode[0]);
                ps.setInt(2, Integer.parseInt(fullDataCode[1]));
                ps.setString(3, fullDataCode[2]);
                ps.setString(4, dataValue.getDataValue() + "");
                ps.setTimestamp(5, new Timestamp(dataDate.getTime()));
                ps.setTimestamp(6, new Timestamp(System.currentTimeMillis()));
            }

            @Override
            public int getBatchSize() {
                return saveList.size();
            }
        };
    }

}
